/**
 * 
 */
package gov.army.workshop.util;

import java.io.FileInputStream;

import org.apache.log4j.Logger;
import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.XmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Base DbUnit Test case that initialises test database.
 * 
 * @author Denis Pavlov
 * @author shailendra sinha
 * @since 1.0.0
 * 
 */

public  class HibernateDbUnitTestCase extends DBTestCase {

	protected final Logger LOG = Logger.getLogger(getClass());

	@Autowired
	protected SessionFactory sessionFactory;

	/**
	 * system properties initializing constructor.
	 */
	public HibernateDbUnitTestCase() {
		System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.h2.Driver");
		System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:h2:/tmp/appfuse_light");
		System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "sa");
		System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "");
		System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA,"appfuse_light");
	}

	/**
	 * Start the server.
	 * 
	 * @throws Exception
	 *             in case of startup failure.
	 */
	@Before
	public void setUp() throws Exception {

		H2ServerUtil.getInstance().start("appfuse_light");

		LOG.info("Loading hibernate...");

		super.setUp();
	}

	/**
	 * shutdown the server.
	 * 
	 * @throws Exception
	 *             in case of errors.
	 */
	@After
	public void tearDown() throws Exception {
		// db2Session.close();
		// tdSession.close();
		super.tearDown();
		H2ServerUtil.getInstance().stop();
	}

	/** {@inheritDoc} */
	protected IDataSet getDataSet() throws Exception {
		return new XmlDataSet(new FileInputStream("src/test/resources/sample-data.xml"));
		// return new XlsDataSet(new
		// FileInputStream("src/test/resources/sample-data.xls"));
	}

	/** {@inheritDoc} */
	protected DatabaseOperation getSetUpOperation() throws Exception {
		return DatabaseOperation.REFRESH;
	}

	/** {@inheritDoc} */
	protected DatabaseOperation getTearDownOperation() throws Exception {
		return DatabaseOperation.NONE;
	}

	/**
	 * Demo test to see that the number of user records in the database
	 * corresponds the flat file inserts.
	 */
	@Test
	public void testDemo() {

	}

}
